package com.adcc.ags.cgw.entity.msg;

import com.adcc.ags.cgw.entity.BaseEntity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * 核心网关消息入库实体
 */
public class TrafficMsg extends BaseEntity {

    // id
    private String id = Strings.nullToEmpty("");

    // 类型
    private String type = Strings.nullToEmpty("");

    // 发送者
    private String sender = Strings.nullToEmpty("");

    // 接收者
    private String receiver = Strings.nullToEmpty("");

    // 报文原文
    private byte[] rawMsg = null;

    // 异常用户
    private String errorUser = Strings.nullToEmpty("");

    // 异常代码
    private String errorCode = Strings.nullToEmpty("");

    /**
     * 构造函数
     * @param cgwMsg
     */
    public TrafficMsg(CGWMsg cgwMsg){
        this.id = cgwMsg.getId();
        this.type = cgwMsg.getType().toString();
        this.sender = cgwMsg.getSender();
        this.receiver = cgwMsg.getReceiver();
        this.rawMsg = cgwMsg.getMsg().getContent();
    }

    /**
     * 构造函数
     * @param id
     * @param receiver
     */
    public TrafficMsg(String id,String receiver){
        this.id = id;
        this.receiver = receiver;
    }

    /**
     * 构造函数
     * @param id
     * @param errorCode
     * @param errorUser
     */
    public TrafficMsg(String id,String errorCode,String errorUser){
        this.id = id;
        this.errorCode = errorCode;
        this.errorUser = errorUser;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public byte[] getRawMsg() {
        return rawMsg;
    }

    public void setRawMsg(byte[] rawMsg) {
        this.rawMsg = rawMsg;
    }

    public String getErrorUser() {
        return errorUser;
    }

    public void setErrorUser(String errorUser) {
        this.errorUser = errorUser;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    @Override
    public Optional<BaseEntity> formatJson(String json) throws Exception {
        return null;
    }

    @Override
    public String toJSON() throws JsonProcessingException {
        Map map = Maps.newHashMapWithExpectedSize(3);
        map.put("receiver",receiver);
        map.put("errorCode",errorCode);
        map.put("errorUser",errorUser);
        return new ObjectMapper().writeValueAsString(map);
    }
}
